package org.androidpn.activity;

import org.androidpn.R;
import org.androidpn.client.Constants;
import org.androidpn.http.HttpApi;
import org.androidpn.model.CUser;
import org.androidpn.model.NetModel;
import org.androidpn.utils.ConstanceUtil;
import org.androidpn.utils.JsonUtil;
import org.androidpn.utils.ToastUtil;

import com.android.volley.VolleyError;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

public class SubmitSecurityCodeActivity extends Activity implements OnClickListener {
	private ImageView ivBack;
	private TextView tvRegister;
	private String phoneNumber;
	private String password;
	private int type;
	private String deviceid;
	private CUser user;
	private String code;
	private EditText etCode;

	// 倒计时
	private int countdown = 60;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_submit_security_code);
		ivBack = (ImageView) findViewById(R.id.iv_back);
		tvRegister = (TextView) findViewById(R.id.tv_register);
		etCode = (EditText) findViewById(R.id.et_code);
		ivBack.setOnClickListener(this);
		tvRegister.setOnClickListener(this);
		tvRegister.setClickable(false);
		phoneNumber = this.getIntent().getStringExtra("phone_number");
		password = this.getIntent().getStringExtra("password");
		type = this.getIntent().getIntExtra("type", ConstanceUtil.TYPE_BINGREN);
		code = this.getIntent().getStringExtra("code");
		// etCode.setText(code);
		SharedPreferences sharedPrefs = getSharedPreferences(Constants.SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE);
		deviceid = sharedPrefs.getString(Constants.XMPP_USERNAME, "");
		Log.e("wulianshu", "deviceidS user.getDeviceid()" + deviceid);
		etCode.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				// TODO Auto-generated method stub

			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {
				// TODO Auto-generated method stub

			}

			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub
				if (etCode.getText().toString().length() == 4) {
					tvRegister.setClickable(true);
				} else {
					tvRegister.setClickable(false);
				}
			}
		});
		// Timer timer = new Timer();
		// TimerTask task =new TimerTask() {
		// @Override
		// public void run() {
		// // TODO Auto-generated method stub
		//
		// }
		// };
		// timer.schedule(task, 0, 1000);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.menu_submit_security_code, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		// noinspection SimplifiableIfStatement
		if (id == R.id.action_settings) {
			return true;
		}

		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		switch (arg0.getId()) {
		case R.id.iv_back:
			this.finish();
			break;
		case R.id.tv_register:
			if (!code.equals(etCode.getText().toString())) {
				ToastUtil.showShortToast(SubmitSecurityCodeActivity.this, "验证码不正确");
				return;
			}
			user = new CUser();
			user.setPassword(password);
			user.setTelephone(phoneNumber);
			user.setType(type);
			user.setDeviceid(deviceid);

			MyResponseListener listener = new MyResponseListener();
			HttpApi.register(user, listener, listener);
			break;
		default:
			break;
		}
	}

	class MyResponseListener
			implements com.android.volley.Response.Listener<String>, com.android.volley.Response.ErrorListener {

		@Override
		public void onResponse(String response) {
			// TODO Auto-generated method stub
			NetModel netModel = JsonUtil.parseObject(response, NetModel.class);
			ToastUtil.showShortToast(SubmitSecurityCodeActivity.this, netModel.getContent());
			if (netModel.getCode() == ConstanceUtil.REQUEST_SUCCESS) {
				Intent intent = new Intent(SubmitSecurityCodeActivity.this, CompleteUserinfoActivity.class);
				intent.putExtra("cuser", user);
				startActivity(intent);
				finish();
			}
		}

		@Override
		public void onErrorResponse(VolleyError error) {
			// TODO Auto-generated method stub
			ToastUtil.showShortToast(SubmitSecurityCodeActivity.this, "网络异常");
		}
	}
}
